package com.golte.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;

/**
 * @author sm
 * @version 1.0
 */
@Slf4j
@Component
public class OssUploadUtil {
    public static final String END_POINT = "oss-cn-shanghai.aliyuncs.com";
    public static final String ACCESS_KEY_ID = "LTAI5t6SUwk4aiU8jWVkwNT6";
    public static final String ACCESS_KEY_SECRET = "8vjoNEVsIv2JiYtIgaRzYPWM9GqQok";

    public String upload(MultipartFile file) throws Exception{
        String baseUrl = "https://gilte-menhu.oss-cn-shanghai.aliyuncs.com/";
        log.info("上传Oss地址:"+baseUrl);
        OSS ossClient = new OSSClientBuilder().build(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        log.info("上传Oss  ACCESS_KEY_ID="+ACCESS_KEY_ID+"  ACCESS_KEY_SECRET="+ACCESS_KEY_SECRET);
        InputStream inputStream = file.getInputStream();
        String fileName = "golte-data-zhongtai/" + System.currentTimeMillis() + file.getOriginalFilename();
        log.info("开始上传======================");
        ossClient.putObject("gilte-menhu", fileName, inputStream);
        ossClient.shutdown();
        return baseUrl + fileName;
    }

}
